#include <iostream>
#include <vector>
#include <memory>
#include <functional>
#include <cmath>

// 形状接口
class Shape {
public:
    virtual ~Shape() = default;
    virtual double area() const = 0;
    virtual bool isValid() const = 0;
};

// 长方形类
class Rectangle : public Shape {
private:
    double width;
    double height;

public:
    Rectangle(double w, double h) : width(w), height(h) {}

    double area() const override {
        return width * height;
    }

    bool isValid() const override {
        return width > 0 && height > 0;
    }
};

// 正方形类
class Square : public Shape {
private:
    double side;

public:
    Square(double s) : side(s) {}

    double area() const override {
        return side * side;
    }

    bool isValid() const override {
        return side > 0;
    }
};

// 三角形类
class Triangle : public Shape {
private:
    double a;
    double b;
    double c;

public:
    Triangle(double x, double y, double z) : a(x), b(y), c(z) {}

    double area() const override {
        double s = (a + b + c) / 2;
        return std::sqrt(s * (s - a) * (s - b) * (s - c));
    }

    bool isValid() const override {
        return a > 0 && b > 0 && c > 0 && (a + b > c) && (a + c > b) && (b + c > a);
    }
};

// 简单工厂
class ShapeFactory {
public:
    static std::shared_ptr<Shape> createShape(int type, double a, double b = 0, double c = 0) {
        switch (type) {
            case 1:
                return std::make_shared<Rectangle>(a, b);
            case 2:
                return std::make_shared<Square>(a);
            case 3:
                return std::make_shared<Triangle>(a, b, c);
            default:
                throw std::invalid_argument("Invalid shape type");
        }
    }
};

// 泛型链表节点
template <typename T>
class Node {
public:
    Node<T>* next;
    T data;

    Node(T t) : next(nullptr), data(t) {}
};

// 泛型链表类
template <typename T>
class GenericList {
private:
    Node<T>* head;
    Node<T>* tail;

public:
    GenericList() : head(nullptr), tail(nullptr) {}

    ~GenericList() {
        Node<T>* current = head;
        while (current != nullptr) {
            Node<T>* next = current->next;
            delete current;
            current = next;
        }
    }

    Node<T>* getHead() const {
        return head;
    }

    void Add(T t) {
        Node<T>* n = new Node<T>(t);
        if (tail == nullptr) {
            head = tail = n;
        } else {
            tail->next = n;
            tail = n;
        }
    }

    void ForEach(std::function<void(T)> action) const {
        Node<T>* current = head;
        while (current != nullptr) {
            action(current->data);
            current = current->next;
        }
    }
};

// 闹钟事件机制
class AlarmClock {
private:
    int time;
    int alarmTime;

public:
    AlarmClock(int currentTime, int setAlarmTime) : time(currentTime), alarmTime(setAlarmTime) {}

    void tick() {
        time++;
        onTick();
        if (time == alarmTime) {
            onAlarm();
        }
    }

    void onTick() const {
        std::cout << "Tick: " << time << std::endl;
    }

    void onAlarm() const {
        std::cout << "Alarm! It's " << alarmTime << " now." << std::endl;
    }
};

int main() {
    // 创建形状对象
    std::vector<std::shared_ptr<Shape>> shapes;
    shapes.push_back(ShapeFactory::createShape(1, 5, 3)); // 长方形
    shapes.push_back(ShapeFactory::createShape(2, 4));     // 正方形
    shapes.push_back(ShapeFactory::createShape(3, 3, 4, 5)); // 三角形

    // 计算面积之和
    double totalArea = 0;
    for (const auto& shape : shapes) {
        if (shape->isValid()) {
            totalArea += shape->area();
        }
    }
    std::cout << "Total Area: " << totalArea << std::endl;

    // 使用泛型链表类存储整数
    GenericList<int> intList;
    for (int x = 0; x < 10; x++) {
        intList.Add(x);
    }

    // 使用ForEach打印链表元素，求最大值、最小值和求和
    int max = INT_MIN, min = INT_MAX, sum = 0;
    intList.ForEach([&](int data) {
        std::cout << "Data: " << data << std::endl;
        if (data > max) max = data;
        if (data < min) min = data;
        sum += data;
    });

    std::cout << "Max: " << max << std::endl;
    std::cout << "Min: " << min << std::endl;
    std::cout << "Sum: " << sum << std::endl;

    // 闹钟示例
    AlarmClock clock(0, 5); // 当前时间为0，设置闹钟时间为5
    for (int i = 0; i < 6; i++) {
        clock.tick();
    }

    return 0;
}
